#-*- Makefile -*-

## Defaults for user settings.  See MkRules.dist for descriptions.

LOCAL_SOFT ?= $(R_HOME)/extsoft
EXT_LIBS ?= $(LOCAL_SOFT)
G_FLAG ?= -gdwarf-2
USE_ATLAS ?= NO
ATLAS_PATH ?= 
MULTI ?=
TOOL_PATH ?=
BINPREF ?= c:/rtools40/mingw32/bin/
BINPREF64 ?= c:/rtools40/mingw64/bin/
COMPILED_BY ?= gcc-$(shell $(CC) --version | egrep -o "([0-9]{1,}\.){2,}[0-9]{1,}")
M_ARCH ?=
AS_ARCH ?=
RC_ARCH ?=
DT_ARCH ?=
WIN ?= 32
ICU_PATH ?= 
ICU_LIBS ?= -lsicuin -lsicuuc -lsicudt -lstdc++
CAIRO_HOME ?=
USE_CAIRO ?= $(if $(CAIRO_HOME),YES)
CAIRO_CPPFLAGS ?= "-I$(CAIRO_HOME)/win$(WIN)"
CAIRO_LIBS ?= "-L$(CAIRO_HOME)/win$(WIN) -lcairo -lpixman-1 -lpng -lz -lgdi32 -lmsimg32"
CURL_CA_BUNDLE ?= /usr/ssl/certs/curl-ca-bundle.crt
BUILD_HTML ?= NO
MIKTEX ?= TRUE
TEXI2ANY ?= missing
TEXI2DVI ?= texi2dvi
EOPTS ?= -mfpmath=sse -msse2 -mstackrealign
OPENMP ?= -fopenmp
PTHREAD ?= -pthread
ISDIR ?= C:/packages/Inno
QPDF ?=
HOME32 ?=
DLLs32 ?=
DLLs64 ?=
WIX3DIR ?= C:/packages/WiX3.5/bin
WIX_PERSONAL ?= 0

## =============== end of user setting defaults  ===================


ifeq "$(MULTI)" "64"
WIN = 64
M_ARCH = -m64
AS_ARCH = --64
RC_ARCH = -F pe-x86-64
DT_ARCH = -m i386:x86-64 --as-flags --64
BINPREF64 = $(TOOL_PATH)
endif
ifeq "$(MULTI)" "32"
WIN = 32
# these are normally the defaults
M_ARCH = -m32
AS_ARCH = --32
RC_ARCH = -F pe-i386
DT_ARCH = -m i386 --as-flags --32
BINPREF = $(TOOL_PATH)
endif


ifeq "$(WIN)" "64"
R_ARCH = /x64
arch_DEFS = -DR_ARCH='"x64"'
BINPREF = $(BINPREF64)
SYMPAT = 's/^.* [BCDRT] / /p'
NM_FILTER ?= | $(SED) -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d'
else
R_ARCH = /i386
arch_DEFS = -DR_ARCH='"i386"'
SYMPAT = 's/^.* [BCDRT] _/ /p'
endif

## -std=c99 would force __STRICT_ANSI__ disabling strdup etc, and also alloca
C99FLAG=-std=gnu99

## earlier Rtools needed -sjlj; the alternative is -dw2, which failed building R.
GCC4_SUFF=

BINDIR=bin$(R_ARCH)

ifdef USE_IMPLIBS
IMPDIR=lib
else
IMPDIR=$(BINDIR)
endif

AR=$(BINPREF)ar
CAT=cat
CC=$(BINPREF)gcc$(GCC4_SUFF) $(C99FLAG) $(M_ARCH)
CP=cp
# as set by make
CPP=$(CC) -E
DLL=$(CC)
DLLTOOL=$(BINPREF)dlltool
## some versions have trouble with paths, so we always need this
## even for native builds
DLLTOOLFLAGS=--as $(BINPREF)as $(DT_ARCH) -k
ECHO=echo
F77=$(BINPREF)gfortran$(GCC4_SUFF) $(M_ARCH)
FLIBS=-lgfortran -lquadmath
LINKER=$(MAIN_LD)
MAIN_LD=$(CC)
## MAKEINFO = $(TEXI2ANY)
MKDIR=mkdir
NM=$(BINPREF)nm
RANLIB=$(BINPREF)ranlib
RESCOMP=$(BINPREF)windres $(RC_ARCH)

## If the Fortran compiler is gfortran >= 7, add
## -fno-optimize-sibling-calls to avoid recent gfortran optimizations
## that break with LAPACK/BLAS-style passing of length-1 strings
## (without hidden arguments giving their lengths).
GFVER = $(shell echo "__GNUC__" | $(F77) -E -P -)
GF7OPTS =
ifneq ($(GFVER),"")
GF7OPTS = $(shell [ $(GFVER) -ge 7 ] && echo "-fno-optimize-sibling-calls")
endif

# as set by make
RM=rm -f
SED=sed
SORT=sort

.SUFFIXES:
.SUFFIXES: .c .cc .cpp .C .f .o .a .def .exp .dll .exe .d .f90 .f95 .m .S

.c.d:
	@echo "making $@ from $<"
	@$(CC) -MM $(CPPFLAGS) $($*-CPPFLAGS) $(DEFINES) $< -o $@

.c.o:
	$(CC) $(CPPFLAGS) $($*-CPPFLAGS) $(CFLAGS) $($*-CFLAGS) $(DEFINES) -c $< -o $@

.f.o:
	$(F77) $(FFLAGS) $($*-FFLAGS) -c $< -o $@

%.exe:
	$(LINKER) $(LINKFLAGS) $($*-LINKFLAGS) -o $@ $^ $($*-LIBS) $(LIBS)

%.dll:
	@$(ECHO) EXPORTS > $*.def
	@$(NM) $^ | $(SED) -n $(SYMPAT) $(NM_FILTER) >> $*.def
	$(DLL) -shared $(DLLFLAGS) $($*-DLLFLAGS) -o $@ $*.def $^ $($*-DLLLIBS) $(DLLLIBS)
	@$(RM) $*.def

lib%.dll.a: %.def
	$(DLLTOOL) $(DLLTOOLFLAGS) $($*-DLLTOOLFLAGS) --dllname $*.dll --input-def $*.def --output-lib $@

%.a:
#	@$(ECHO) -------- Building $@ from $^ --------
	@$(RM) $@
	$(AR) crs $@ $^

%.o: %.rc
	$(RESCOMP) $(RESFLAGS) $($*-RESFLAGS) -i $< -o $@
